from midiutil.MidiFile import MIDIFile

try:
    from collections import OrderedDict
except ImportError:
    OrderedDict = dict

trackNums = 8
trackNames = [	'Drums',
		'ElPckBass',
		'DistGuitar',
		'ElClnGuitar',
		'StringsEns1',
		'Extra1',
		'Extra2',
		'Extra3'
		]

inst_dict = {	'Drums': 118,
		'ElPckBass': 34,
		'DistGuitar1': 30,
		'ElClnGuitar': 27,
		'StringEns1': 48,
		'Extra1': 0,
		'Extra2': 0,
		'Extra3': 0
		}

drum_dict = {	'Kick': 36,
		'Snare': 38,
		'HHClosed': 42, 
		'HHOpen': 46,
		'Crash': 57
		}

OrderedDict(sorted(drum_dict.items(), key=lambda t: t[0]))

timeSig_numerator = 4
timeSig_denominator = 4
measures = 4

beats = measures * timeSig_denominator
print beats
mf = MIDIFile(trackNums,0,0)
volume = 127
time = 0

# set up the session
track = 0
note = 0.0
for name in trackNames:
	channel = 1
	if name == 'Drums':
		mf.addTrackName(track,time,trackNames[track])
		for drum in drum_dict.keys():
mf.addProgramChange(track,drum_dict.keys().index(drum),time,inst_dict[name])
		mf.addTempo(track,time,140)
		while note < beats:
			if (note % 0.25) == 0:
				print "Adding kick: " + str(note) 
				#mf.addNote(track,drum_dict.keys().index('Kick'),drum_dict.get('Kick'),note,1,100)
			if (note % 1) == 0:
				print "Adding HHOpen: " + str(note)
				#mf.addNote(track,drum_dict.keys().index('HHOpen'),drum_dict.get('HHOpen'),note,1,100)
			if note % 2 == 0:
				print "Adding Snare: " + str(note)
				#mf.addNote(track,drum_dict.keys().index('Snare'),drum_dict.get('Snare'),note,1,100)
			if note % 8 == 0:
				print "Adding Crash: " + str(note)
				mf.addNote(track,drum_dict.keys().index('Crash'),drum_dict.get('Crash'),note,16,volume)
			note += 0.25
	track += 1

# And write it to disk.
binary_midi_file = open("output.mid", 'wb')
mf.writeFile(binary_midi_file)
binary_midi_file.close()

"# test" 
